
//
//  WebVC.m
//  itboyeios_gd
//
//  Created by Apple on 15/10/31.
//  Copyright © 2015年 Boye. All rights reserved.
//

#import "WebVC.h"
#import "NSString+Helper.h"

@interface WebVC ()
{
    UILabel *_loadLabel; // 网页加载提示文字
    UIActivityIndicatorView *_indicatorView; // 网页加载菊花转动
}
@end

@implementation WebVC

// 自定义初始化方法，获取所要显示的网页的网址，并显示该网页
- (instancetype)initWithUrlString:(NSString *)urlString
{
    self = [super init];
    if (self) {
        // 创建网页webView
        CGRect frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64);
        _webView = [[UIWebView alloc] initWithFrame:frame];
        _webView.delegate = self;
        _webView.scalesPageToFit = YES;
        _webView.scrollView.bounces = NO;
        [self.view addSubview:_webView];
        // 发送请求，获取该网页内容
        urlString = [urlString stringByEncodingURLFormat];
        NSURL *url = [NSURL URLWithString:urlString];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
        [_webView loadRequest:request];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //  创建导航栏左边返回按钮，此方法由父类实现
    [self createLeftBarButtonItem];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self isNaviTranslucent:NO statesLightContent:YES];
}


#pragma mark - UIVebView代理方法
// 开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    // 创建提示文字
    _loadLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.width/4, self.view.height*2/5, self.view.width/2, 20)];
    _loadLabel.text = @"加载中……";
    _loadLabel.textAlignment = NSTextAlignmentCenter;
    _loadLabel.textColor = [UIColor lightGrayColor];
    _loadLabel.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:_loadLabel];
    
    // 加载网页时，菊花转动
    _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    CGPoint point = CGPointMake(self.view.width/4, -20);
    _indicatorView.center = point;
    [_loadLabel addSubview:_indicatorView];
    _indicatorView.hidesWhenStopped = YES;
    [_indicatorView startAnimating];
    }

// 加载结束
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // 加载提示移除
    [_indicatorView stopAnimating];
    [_loadLabel removeFromSuperview];
    _loadLabel = nil;
}


#pragma mark - 返回上一界面
// 返回上一界面，覆写父类方法
- (void)backAction
{
    if (_webView.canGoBack) {
        [_webView goBack];
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
